const app = getApp();
const api = require('../../utils/api');
const md5 = require('../../utils/md5');

Page({
    data: {
        form: {
            password: '',
            newPassword: '',
            confirmPassword: '',
        },
        isEdit: false
    },
    onLoad(options) {
        if(options.type) {
            this.setData({
                isEdit: true
            });
        }
    },
    onShow() {

    },

    bindinput(e) { //输入框监听
        let item = e.currentTarget.dataset.item;
        let val = e.detail.value;

        this.setData({
            [item]: val
        });
    },
    validForm() { //表单校验
        if(!this.data.form.password) {
            wx.showToast({
                title: `请输入${this.data.isEdit ? '原支付密码' : '支付密码'}`,
                icon: 'none'
            });
            return true;
        }
        if(this.data.isEdit && !this.data.form.newPassword) {
            wx.showToast({
                title: `请输入新支付密码`,
                icon: 'none'
            });
            return true;
        }
        if((!this.data.isEdit && this.data.form.password.length < 6) || (this.data.isEdit && this.data.form.newPassword.length < 6)) {
            wx.showToast({
                title: `支付密码长度不能少于6位`,
                icon: 'none'
            });
            return true;
        }
        if(!this.data.form.confirmPassword) {
            wx.showToast({
                title: `请再次输入${this.data.isEdit ? '新支付密码' : '支付密码'}`,
                icon: 'none'
            });
            return true;
        }
        if((!this.data.isEdit && this.data.form.confirmPassword != this.data.form.password) || (this.data.isEdit && this.data.form.confirmPassword != this.data.form.newPassword)) {
            wx.showToast({
                title: `两次输入的支付密码不一致`,
                icon: 'none'
            });
            return true;
        }
    },
    submit() { //提交
        if(this.validForm()) {
            return;
        }

        let pwdFirst = this.data.form.password;
        let pwdSecond = this.data.isEdit ? this.data.form.newPassword : this.data.form.confirmPassword;
        let pwdThird = this.data.form.confirmPassword;
        let url = `/user/pay/set?pwdFirst=${md5.md5(pwdFirst)}&pwdSecond=${md5.md5(pwdSecond)}`;
        if(this.data.isEdit) {
            url = url + `&pwdThird=${md5.md5(pwdThird)}`;
        }
        api.post(url)
            .then(() => {
                wx.showToast({
                    title: `密码${this.data.isEdit ? '修改' : '设置'}成功！`,
                    icon: 'success'
                });
                if(!this.data.isEdit) {
                    //更新个人信息
                    api.post('/user/info')
                        .then(res => {
                            wx.setStorageSync('userinfo', res);
                            app.globalData.userInfo = res;

                            setTimeout(() => {
                                wx.navigateBack(-1);
                            }, 1000);
                        })
                } else {
                    setTimeout(() => {
                        wx.navigateBack(-1);
                    }, 1200);
                }
            })
    },
})